package com.aube.interact.taste.goods;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.animation.Interpolator;

import java.lang.reflect.Field;

/**
 * Created by hill on 16/12/28.
 */

public class CustomDurationViewPager extends ViewPager {

    public CustomDurationViewPager(Context context) {
        super(context);
        postInitViewPager();
    }

    public CustomDurationViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        postInitViewPager();
    }

    private CustomViewPagerScroller mScroller = null;

    /**
     * Override the Scroller instance with our own class so we can change the
     * duration
     */
    private void postInitViewPager() {
        try {
            Field scroller = ViewPager.class.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
            interpolator.setAccessible(true);

            mScroller = new CustomViewPagerScroller(getContext(),
                    (Interpolator) interpolator.get(null));
            scroller.set(this, mScroller);
        } catch (Exception e) {
        }

//        try {
//            Field scroller = ViewPager.class.getDeclaredField("mScroller");
//            scroller.setAccessible(true);
//            Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
//            interpolator.setAccessible(true);
//
//            mScroller = new CustomViewPagerScroller(mContext, (Interpolator) interpolator.get(null));
//            scroller.set(this, mScroller);
//        } catch (Exception e) {
//            Utils.Log(TAG, "[initView] exception : " + e);
//        }
    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
        mScroller.setScrollDurationFactor(scrollFactor);
    }

    public void setScrollDuration(int duration) {
        mScroller.setTime(duration);
    }

}